package com.car.ljc.util;

import cn.hutool.core.date.DateUtil;
import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONObject;
import com.alibaba.fastjson.JSON;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;

/**
 * @Author ljc
 * @Date 2018/9/20 11:34
 */
public class CommonUtil {
    /**
     * 向指定URL发送GET方法的请求
     *
     */
    public static String get(String url) {
        BufferedReader in = null;
        try {
            URL realUrl = new URL(url);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent", "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            connection.setConnectTimeout(5000);
            connection.setReadTimeout(5000);
            // 建立实际的连接
            connection.connect();
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
            StringBuffer sb = new StringBuffer();
            String line;
            while ((line = in.readLine()) != null) {
                sb.append(line);
            }
            return sb.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return null;
    }

    /**
     * 构建token和时间参数
     * @param token
     * @return
     */
    public static String buildTokenAndTime(String token){
      return String.format("token=%s&now=%s",token,System.currentTimeMillis());
    }
    public static int obj2Int(Object object){
        return Integer.valueOf(object.toString());
    }

    public static void main(String[] args) {
        long time = System.currentTimeMillis();
        String body = HttpRequest.post("https://h5.chiguawan.com/Emperor/user/saveUserData")
                //.body(new JSONObject("{\"saveTime\":"+time+",\"coin\":\"28571400\",\"diamon\":600,\"shopLevel\":20,\"isNewPlayer\":false,\"speedup\":1,\"upStartTime\":"+time+",\"loginDays\":1,\"loginRewardDays\":1,\"lastLoginTime\":1539265106000,\"guideStep\":4,\"friendDraw\":0,\"shareCoinNum\":0,\"shareDiamonNum\":0,\"luckyCount\":0,\"luckyShareCount\":0,\"luckyUpTime\":0,\"dbVersion\":1025,\"version\":\"1.2.2\",\"slots\":[{\"id\":6,\"pos\":0},{\"id\":4,\"pos\":1},{\"id\":1,\"pos\":2},{\"id\":2,\"pos\":3},{\"id\":3,\"pos\":4}],\"items\":[{\"id\":1,\"buyUnlock\":1,\"diamonUnlock\":1,\"coinUnlock\":1,\"coinCount\":35,\"diamonCount\":0},{\"id\":2,\"buyUnlock\":1,\"diamonUnlock\":1,\"coinUnlock\":1,\"coinCount\":6,\"diamonCount\":0},{\"id\":3,\"buyUnlock\":1,\"diamonUnlock\":0,\"coinUnlock\":0,\"coinCount\":0,\"diamonCount\":0},{\"id\":4,\"buyUnlock\":1,\"diamonUnlock\":1,\"coinUnlock\":0,\"coinCount\":0,\"diamonCount\":0},{\"id\":5,\"buyUnlock\":1,\"diamonUnlock\":0,\"coinUnlock\":0,\"coinCount\":0,\"diamonCount\":0},{\"id\":6,\"buyUnlock\":1,\"diamonUnlock\":0,\"coinUnlock\":0,\"coinCount\":0,\"diamonCount\":0}],\"shareGrop\":[],\"isFly\":0,\"userid\":\"3005144f1fa-6a36-46e7-b8d4-42ab61cea64e\",\"openId\":\"ovKTi5GoOfkUoVTjApkLTfGwRb78\"}"))
                .body(new JSONObject("{\"saveTime\":1546013882252,\"coin\":\"10000000000000000000000000000000000\",\"diamon\":1020,\"shopLevel\":9,\"isNewPlayer\":false,\"speedup\":1,\"upStartTime\":1546013881252,\"loginDays\":2,\"loginRewardDays\":1,\"lastLoginTime\":1546012814743,\"guideStep\":4,\"friendDraw\":0,\"shareCoinNum\":0,\"shareDiamonNum\":0,\"luckyCount\":0,\"luckyShareCount\":0,\"luckyUpTime\":0,\"dbVersion\":2001271750000000000,\"version\":\"1.2.2\",\"slots\":[{\"id\":9,\"pos\":0},{\"id\":2,\"pos\":2},{\"id\":3,\"pos\":3}],\"items\":[{\"id\":1,\"buyUnlock\":1,\"diamonUnlock\":1,\"coinUnlock\":1,\"coinCount\":36,\"diamonCount\":0},{\"id\":2,\"buyUnlock\":1,\"diamonUnlock\":1,\"coinUnlock\":1,\"coinCount\":15,\"diamonCount\":0},{\"id\":3,\"buyUnlock\":1,\"diamonUnlock\":1,\"coinUnlock\":1,\"coinCount\":17,\"diamonCount\":0},{\"id\":4,\"buyUnlock\":1,\"diamonUnlock\":1,\"coinUnlock\":1,\"coinCount\":16,\"diamonCount\":0},{\"id\":5,\"buyUnlock\":1,\"diamonUnlock\":1,\"coinUnlock\":1,\"coinCount\":0,\"diamonCount\":0},{\"id\":6,\"buyUnlock\":1,\"diamonUnlock\":1,\"coinUnlock\":0,\"coinCount\":0,\"diamonCount\":0},{\"id\":7,\"buyUnlock\":1,\"diamonUnlock\":1,\"coinUnlock\":0,\"coinCount\":0,\"diamonCount\":0},{\"id\":8,\"buyUnlock\":1,\"diamonUnlock\":0,\"coinUnlock\":0,\"coinCount\":0,\"diamonCount\":0},{\"id\":9,\"buyUnlock\":1,\"diamonUnlock\":0,\"coinUnlock\":0,\"coinCount\":0,\"diamonCount\":0}],\"shareGrop\":[],\"isFly\":0,\"userid\":\"3005144f1fa-6a36-46e7-b8d4-42ab61cea64e\",\"openId\":\"ovKTi5GoOfkUoVTjApkLTfGwRb78\"}"))
                .execute().body();
        System.out.println(body);

    }
}
